/*
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 *
 * Copyright @2008 the original author or authors.
 */
package com.bluestone.config;

import java.io.File;
import java.lang.reflect.Constructor;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;

import com.bluestone.BaseAction;
import com.bluestone.report.IExpAttributeHandler;
import com.bluestone.scripts.ActionScript;
import com.bluestone.util.Util;

/**
 * @author <a href="mailto:ming7655@gmail.com">Aaron</a>
 *
 * 
 */
public class ConfigUtil {
	private static ConfigUtil INSTANCE;
	private ArrayList<ActionConfig> actions = new ArrayList();
	private ArrayList<AssertorConfig> assertors = new ArrayList();
	private ArrayList<ActionHandlerConfig> actionHandlers = new ArrayList();
	private ArrayList<ExpAttributesHandlerConfig> expAttributesHandlers = new ArrayList();
	private static String configFile = "./bluestone_cfg.xml";
	
	private ConfigUtil(){
		
	}
	public static ConfigUtil getInstance(){
		if(INSTANCE==null){
			INSTANCE = new ConfigUtil();
			//ContextPaser cp = new ContextPaser();
			//cp.parseConfig();
			//print the register informations.
			INSTANCE.registerBluestoneConfig(loadConfigFile(configFile));
			INSTANCE.printRegisterActions();
		}
		return INSTANCE;
	}
	
	private static BluestoneConfig loadConfigFile(String filePath){
		Object obj = Util.loadObjectFromFile(filePath,BluestoneConfig.class);
		if(obj!=null){
			return (BluestoneConfig)obj;
		}else{
			return null;
		}
	}
	private void printRegisterActions(){
		for(ActionConfig ac:actions){
			Util.info("The registered Action: "+ac.getType()+" --> "+ac.getClassName());			
		}
	}
	
	private void printRegisterExpAttriHandlers(){
		for(ExpAttributesHandlerConfig eahc:expAttributesHandlers){
			Util.info("The registered ExpAttributesHandler: "+eahc.getTarget()+" --> "+eahc.getClassName());			
		}
	}
	
	private void printRegisterAssertors(){
		for(AssertorConfig ac:assertors){
			Util.info("The registered assertor: "+ac.getName()+" --> "+ac.getClassName());			
		}
	}
	
	private void printRegistActinoHandlers(){
		if(this.actionHandlers==null){
			return;
		}
		for(int i = 0;i<this.actionHandlers.size();i++){
			ActionHandlerConfig ahc = (ActionHandlerConfig)this.actionHandlers.get(i);
			Util.info("The registered ActionHandler: "+ahc.getType()+" --> "+ahc.getClassName());
		}
	}
	
	public void registerActions(List list){
		this.actions.addAll(list);
	}
	
	public void registerAssertors(List list){
		
		this.assertors.addAll(list);
	}
	
	public void registerActionHandlers(List list){
		this.actionHandlers.addAll(list);
	}
	
	public void registerExpAttributesHandlers(List list){
		this.expAttributesHandlers.addAll(list);
	}
	
	public boolean isRegisteredAction(String type){
		String t = type;
		if(type.startsWith("ASSERT")){
			t = "ASSERT";
		}
		for(ActionConfig ac:actions){
			if(ac.getType().equalsIgnoreCase(t)){
				return true;
			}
		}
		return false;
	}
	
	public boolean isRegisteredExpAttrHandler(String target){
		for(ExpAttributesHandlerConfig eahc:expAttributesHandlers){
			if(eahc.getTarget().equalsIgnoreCase(target)){
				return true;
			}
		}
		return false;
	}
	
	public BaseAction getRegisterAction(ActionScript action){
		String type = action.getType().toUpperCase();
		if(type.startsWith("ASSERT")){
			type = "ASSERT";
		}
		if(!this.isRegisteredAction(type)){
			return null;
		}
		for(ActionConfig ac:actions){
		  if(ac.getType().equalsIgnoreCase(type)){
			Object obj = getInstance(ac,action);
			if(BaseAction.class.isInstance(obj)){
				return (BaseAction)obj;
			}
		  }
		}
		return null;
	}
	
	public List getRegisterActions(){
		return this.actions;
	}
	
	public List<AssertorConfig> getRegisterAssertors(){
		return this.assertors;
	}
	
	public IExpAttributeHandler getRegisterExpAtrriHandler(String target){
		if(!this.isRegisteredExpAttrHandler(target)){
			return null;
		}
		for(ExpAttributesHandlerConfig eahc:expAttributesHandlers){
			  if(eahc.getTarget().equalsIgnoreCase(target)){
				Object obj = getInstance(eahc,null);
				if(IExpAttributeHandler.class.isInstance(obj)){
					return (IExpAttributeHandler)obj;
				}
			  }
		}			
	
		return null;
	}
	
	private Object getInstance(AbstractConfig ac,ActionScript action){
		String className = ac.getClassName();
		try {
			Class c= Class.forName(className);
			if(ExpAttributesHandlerConfig.class.isInstance(ac)){
				return c.newInstance();
			}
			Constructor constructor = c.getConstructor(new Class[]{ActionScript.class});
			return constructor.newInstance(new Object[]{action});
		} catch (Exception e) {
			Util.warn(e);
		}		
		return null;
	}
	
	public void registerBluestoneConfig(BluestoneConfig config){
		if(config==null){
			return;
		}
		this.registerActions(config.getActions());
		
	}
	
	public ArrayList getRegisterActionHandlers(){
		ArrayList list = null;
		if(!this.actionHandlers.isEmpty()){
			list =new ArrayList();
			for(int i = 0;i<this.actionHandlers.size();i++){
				ActionHandlerConfig ac = (ActionHandlerConfig)this.actionHandlers.get(i);
				list.add(this.getInstance(ac,null));
			}
		}		
		return list;
	}
	

}
